autocmd!
mapclear
mapclear!
noremap / <Esc>
noremap! / <Esc>
cmap / <C-C>
noremap <F7> /
noremap! <F7> /
map! OP <F1>
map! OQ <F2>
map! OR <F3>
map! OS <F4>
map! [15~ <F5>
map OP <F1>
map OQ <F2>
map OR <F3>
map OS <F4>
map [15~ <F5>
set ignorecase smartcase incsearch backspace=2 linebreak hidden
set autoindent tabstop=4 history=150 mouse=a ttymouse=xterm wildmenu
set wildmode=list:longest,full display=lastline modeline t_Co=256
syntax on
let g:loaded_matchparen=1
hi NonText ctermfg=238
hi SpecialKey ctermfg=238
hi Statement ctermfg=250
hi Constant ctermfg=255
hi Identifier ctermfg=123
hi ErrorMsg ctermbg=143 ctermfg=0
cd /sdcard-ext/vimhome
au VimEnter * if argc() == 0 | e main | endif | so abbrev

set statusline=\ %n\ %{&ff}%y\ %<%F%m%=%5l/%L%4v\ 
hi clear StatusLine
hi StatusLine ctermfg=250 ctermbg=238
nmap K :exe 'se ls='.(&ls==2?0:2)<CR>

func! SetSyntax()
	syntax region txtShadow start=/\~(/ end=/)\~/
	highlight link txtShadow NonText
	syntax region txtTitle start=/^\t/ end=/\n/
	highlight link txtTitle Constant
	syntax region txtBracket start=/\[/ end=/\]/
	highlight link txtBracket Constant
	syntax region txtList start=/{/ end=/}/
	highlight link txtList Constant
endfu
au BufNewFile,BufRead *.txt	call SetSyntax()

let s:MenuList = ["~( vim: set nowrap tw=72 fo=aw: )~",
\	"/mnt/sdcard/download/",
\	"/data/data/org.connectbot/files/kbox/"]
let s:MenuMessage='1. '.s:MenuList[0]
for i in range(1,len(s:MenuList)-1)
	let s:MenuMessage=s:MenuMessage."\n".(i+1).". ".s:MenuList[i]
endfor
func! ShowMyMenu()
	echo s:MenuMessage
	return s:MenuList[nr2char(getchar())-1]
endfu
noremap! <C-R>m <C-R>=ShowMyMenu()<CR>

let s:ScrollMode=0
func! DragStart()
	let s:startline=winline()	
	let s:starttime=reltime()
endfu
func! DragEnd()
	if !s:startline
		return
	endif
	let elapsedtime=reltime(s:starttime)
	let dragspeed=(winline()-s:startline)/(elapsedtime[0]+elapsedtime[1]/1.0e6)
	let lines=min([100,float2nr(dragspeed*dragspeed/64)])
	let sleepcmd='redraw | sleep '.min([100,float2nr(6000/dragspeed/dragspeed)+1]).'m'
	let scrollcmd=dragspeed>0 ? "normal! \<C-Y>" : "normal! \<C-E>"
	if line('.')==line('$') && lines==0
		normal! zb
	elseif lines > 0
		for i in range(1,lines)
			exe scrollcmd
			exe sleepcmd
		endfor
	endif
	let s:startline=0
endfu
func! ToggleScrollMode()
	if !s:ScrollMode
		nnoremap <silent> <leftmouse> <leftmouse>:call DragStart()<CR>
		nnoremap <silent> <leftrelease> <leftmouse>:call DragEnd()<CR>
	else
		nunmap <leftmouse>
		nunmap <leftrelease>
	endif
	let s:ScrollMode= !s:ScrollMode
endfu
nmap `<leftmouse> :call ToggleScrollMode()<CR>
call ToggleScrollMode()

func! SelectBuffer()
	exe 'ls'
	let choice=nr2char(getchar())
	if choice==?'x'
		let bufferlist=split(input("Delete which buffers? "),' ')
		for i in bufferlist 
			exe 'bd! ' . i
		endfor
	elseif choice!='/'
		redraw "Suppress Press Enter prompt
		exe 'b ' . choice
	endif
endfu
cnorea <expr> ls ((getcmdtype()==':' && getcmdpos()<=3)? 'call SelectBuffer()' : 'ls')

func! CapWait(prev)
	redraw
	let input = nr2char(getchar())
	if input=~'[.?!\r\n[:blank:]]'
		exe 'normal! i' . input . "\<Right>"
		return CapWait(input)
	else
		if input=~'[\e\/]' 
			return "\<del>"
		elseif a:prev=~'[\r\n[:blank:]]'
			return toupper(input) . "\<del>"
		else 
			return input . "\<del>"
		endif
	endif
endfu
func! CapHere()
	let trunc = getline(".")[0:col(".")-2] 
	return col(".")==1 ? CapWait("\r") :
	\	trunc=~'[?!.]\s*$\|^\s*$' ? CapWait(trunc[-1:-1]) :
	\	"\<del>"
endfu
imap <silent> . ._<Left><C-R>=CapWait('.')<CR>
imap <silent> ? ?_<Left><C-R>=CapWait('?')<CR>
imap <silent> ! !_<Left><C-R>=CapWait('!')<CR>
imap <silent> <CR> <CR>_<Left><C-R>=CapWait("\r")<CR>
imap <silent> <NL> <NL>_<Left><C-R>=CapWait("\n")<CR>
nmap <silent> O O_<Left><C-R>=CapWait("\r")<CR>
nmap <silent> o o_<Left><C-R>=CapWait("\r")<CR>
nmap <silent> a a_<Left><C-R>=CapHere()<CR>
nmap <silent> A A_<Left><C-R>=CapHere()<CR>
nmap <silent> i i_<Left><C-R>=CapHere()<CR>
nmap <silent> I I_<Left><C-R>=CapHere()<CR>
